package com.zxy.supplier_system.server.utils.wechat.official

import com.zxy.supplier_system.server.utils.wechat.WechatApiException
import org.springframework.http.ResponseEntity
import org.springframework.web.client.getForEntity

fun WechatOfficialAccountHelper.getAccessToken(): ResponseEntity<GetAccessTokenResponse> {
    val responseEntity = restTemplate.getForEntity<GetAccessTokenResponse>(
        "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret"
    )
    if (responseEntity.body?.errcode!=null && responseEntity.body?.errcode!=0){
        throw WechatApiException(responseEntity.body!!.errcode!!,responseEntity.body!!.errmsg)
    }
    return responseEntity
}

data class GetAccessTokenResponse(
    val access_token: String?,
    val expires_in: Int?,
    val errcode: Int?,
    val errmsg: String?
)
